home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1993…ch: Other People's Memory / ADC Developer CD (1993-03) (''Other People's Memory'')_iso / Dev.CD Mar 93.iso / Technical Documentation / Sample Code / DTS.Lib & Samples / Kibitz / Print.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-22  |  8.2 KB  |  307 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** MultiFinder-Aware Shell Application
  5. **
  6. ** File:        print.c
  7. ** Written by:  Eric Soldan
  8. ** Based on:    Code from Pete "Luke" Alexander.
  9. **
  10. ** Copyright © 1989-1992 Apple Computer, Inc.
  11. ** All rights reserved. */
  12.  
  13.  
  14.  
  15. /*****************************************************************************/
  16.  
  17.  
  18.  
  19. #include "Kibitz.h"                /* Get the Kibitz includes/typedefs, etc.    */
  20. #include "KibitzCommon.h"        /* Get the stuff in common with rez.        */
  21. #include "Kibitz.protos"        /* Get the prototypes for Kibitz.            */
  22.  
  23. #ifndef __ERRORS__
  24. #include <Errors.h>
  25. #endif
  26.  
  27. #ifndef __RESOURCES__
  28. #include <Resources.h>
  29. #endif
  30.  
  31.  
  32.  
  33. /*****************************************************************************/
  34.  
  35.  
  36.  
  37. pascal void            PrintIdleProc(void);
  38. short                gPrintPage;
  39. static DialogPtr    PrintingStatusDialog;
  40.  
  41.  
  42.  
  43. /*****************************************************************************/
  44.  
  45.  
  46.  
  47. /* This print-loop function is designed to be called under various situations.
  48. ** The big issue that it handles is finder printing.  If multiple documents
  49. ** are to be printed from the finder, the user should only see one job dialog
  50. ** for all the files.  (If a job dialog is shown for each file, how does the
  51. ** user know for which file the dialog is for?)  So, for situations where
  52. ** there is more than one file to be printed, call this code the first time
  53. ** with the firstJob boolean true.  Normally, the jobDlg boolean will also
  54. ** be true, except that under 7.0, you may be printing in the background.
  55. ** If this is the case, you don't want a job dialog for even the first file,
  56. ** and you should pass in false for the jobDlg boolean in this case.  For
  57. ** files 2-N, you should pass false for both booleans.  For regular application
  58. ** printing, you should pass true for both booleans, since the file is the
  59. ** first (only) file, and you are not in the background.
  60. **
  61. ** After calling this function to print a document, you need to call it
  62. ** again with a nil document handle.  The print record for the first (or only)
  63. ** document printed is preserved in a static variable.  This is so that the
  64. ** job dialog information can be passed on to documents 2-N in the print job.
  65. ** Calling this function with the document handle nil tells this function
  66. ** that you are done printing documents, and that the print record for the
  67. ** first job can be disposed of. */
  68.  
  69. #pragma segment Print
  70. OSErr    AppPrintDocument(FileRecHndl frHndl, Boolean jobDlg, Boolean firstJob)
  71. {
  72.     OSErr            err;
  73.     THPrint            prRecHndl;
  74.     TPPrPort        printPort;
  75.     GrafPtr            oldPort;
  76.     short            i, keepResFile, copies, fstPage, lstPage;
  77.     TPrStatus        status;
  78.     ControlHandle    proceedButton;
  79.     Rect             rct;
  80.  
  81.     static THPrint    prMergeHndl;
  82.  
  83.     if (!frHndl) {
  84.         if (prMergeHndl) {
  85.             DisposHandle((Handle)prMergeHndl);
  86.             prMergeHndl = nil;
  87.         }
  88.         return(noErr);
  89.     }
  90.  
  91.     PrintingStatusDialog = nil;
  92.  
  93.     if (!(prRecHndl = (THPrint)NewHandle(sizeof(TPrint)))) return(memFullErr);
  94.         /* If we can't generate a print record handle, we are out of here. */
  95.  
  96.     BlockMove((Ptr)&((*frHndl)->doc.print), (Ptr)(*prRecHndl), sizeof(TPrint));
  97.         /* Get the document's print info into the print record handle. */
  98.  
  99.     GetPort(&oldPort);
  100.  
  101.     DoSetCursor(&qd.arrow);
  102.     PrOpen();
  103.     err = PrError();
  104.  
  105.     if (!err) {
  106.         keepResFile = CurResFile();
  107.  
  108.         if (!(*frHndl)->doc.printRecValid) {
  109.             PrintDefault(prRecHndl);            /* The document print record was never 
  110.             err = PrError();                    ** initialized.  Now is is. */
  111.         }            
  112.         if (!err) {
  113.             PrValidate(prRecHndl);        /* Do this just 'cause Apple says so. */
  114.             err = PrError();
  115.         }
  116.         if (!err) {
  117.             if (jobDlg) {                /* User gets to click some buttons. */
  118.                 if (!(PrJobDialog(prRecHndl))) err = userCanceledErr;
  119.                 else                           err = PrError();
  120.             }
  121.         }
  122.         if (!err) {
  123.             if (!firstJob) {
  124.                 fstPage = (*prMergeHndl)->prJob.iFstPage;
  125.                 lstPage = (*prMergeHndl)->prJob.iLstPage;
  126.                 PrJobMerge(prMergeHndl, prRecHndl);
  127.                 (*prMergeHndl)->prJob.iFstPage = (*prRecHndl)->prJob.iFstPage = fstPage;
  128.                 (*prMergeHndl)->prJob.iLstPage = (*prRecHndl)->prJob.iLstPage = lstPage;
  129.                 err = PrError();
  130.             }
  131.         }
  132.  
  133.         if (!err) {            /* Put the defaulted/validated/jobDlg'ed print record in the doc. */
  134.             fstPage = (*prRecHndl)->prJob.iFstPage;
  135.             lstPage = (*prRecHndl)->prJob.iLstPage;
  136.             copies  = (*prRecHndl)->prJob.iCopies;
  137.             BlockMove((Ptr)(*prRecHndl), (Ptr)&((*frHndl)->doc.print), sizeof(TPrint));
  138.             (*frHndl)->doc.printRecValid = true;
  139.  
  140.             ParamText((*frHndl)->fileState.fss.name, nil, nil, nil);
  141.             PrintingStatusDialog = GetNewDialog(rPrStatusDlg, nil, (WindowPtr)-1);
  142.             if (PrintingStatusDialog) {
  143.                 GetDItem(PrintingStatusDialog, 1, &i, (Handle *)&proceedButton, &rct);
  144.                 HiliteControl(proceedButton, 255);
  145.                     /* Setup the proceed/pause/cancel dialog with the document name. */
  146.                 (*prRecHndl)->prJob.pIdleProc = PrintIdleProc;
  147.                 UseResFile(keepResFile);
  148.                     /* Hook in the proceed/pause/cancel dialog. */
  149.             }
  150.  
  151.             for (i = 1; (i <= copies) && (!err); ++i) {
  152.  
  153.                 printPort = PrOpenDoc(prRecHndl, nil, nil);
  154.                 if (!(err = PrError())) {
  155.  
  156.                     gPrintPage = 1;
  157.                     while (gPrintPage <= lstPage) {
  158.  
  159.                         PrOpenPage(printPort, nil);
  160.  
  161.                         if (!(err = PrError()))
  162.                             ImageDocument(frHndl, false);
  163.                                 /* Do the print thing here. */
  164.  
  165.                         PrClosePage(printPort);
  166.  
  167.                         if (!gPrintPage) break;
  168.                         ++gPrintPage;
  169.                     }
  170.                     gPrintPage = 0;
  171.                     PrCloseDoc(printPort);
  172.                 }
  173.             }
  174.         }
  175.  
  176.         if (
  177.             (!err) &&
  178.             ((*prRecHndl)->prJob.bJDocLoop == bSpoolLoop) &&
  179.             (!(err = PrError()))
  180.         ) {
  181.             PrPicFile(prRecHndl, nil, nil, nil, &status);
  182.             err = PrError();
  183.         }
  184.     }
  185.  
  186.     if (firstJob) prMergeHndl = prRecHndl;
  187.     else          DisposHandle((Handle)prRecHndl);
  188.  
  189.     if (PrintingStatusDialog) DisposDialog(PrintingStatusDialog);
  190.  
  191.     PrClose();
  192.     SetPort(oldPort);
  193.  
  194.     return(err);
  195. }
  196.  
  197.  
  198.  
  199. /*****************************************************************************/
  200.  
  201.  
  202.  
  203. /* PrintIdleProc will handle events in the 'Printing Status Dialog' which
  204. ** gives the user the option to 'Proceed', 'Pause', or 'Cancel' the current
  205. ** printing job during print time.
  206. **
  207. ** The buttons:
  208. **        1: Proceed
  209. **        2: Pause
  210. **        3: Cancel  */
  211.  
  212. #pragma segment Print
  213. pascal void        PrintIdleProc(void)
  214. {
  215.     Boolean                button, paused;
  216.     ControlHandle        pauseButton, proceedButton;
  217.     DialogPtr            aDialog;
  218.     EventRecord            anEvent;
  219.     GrafPtr                oldPort;
  220.     Rect                 rct;
  221.     short                item, itemType, keepResFile;
  222.  
  223.     GetPort(&oldPort);
  224.  
  225.     UseResFile(keepResFile = CurResFile());
  226.  
  227.     GetDItem(PrintingStatusDialog, 1, &itemType, (Handle *)&proceedButton, &rct);
  228.     HiliteControl(proceedButton, 255);
  229.     GetDItem(PrintingStatusDialog, 2, &itemType, (Handle *)&pauseButton, &rct);
  230.  
  231.     paused = false;
  232.     do {
  233.         if (GetNextEvent((mDownMask + mUpMask + updateMask), &anEvent)) {
  234.             if (PrintingStatusDialog != FrontWindow ())
  235.             SelectWindow(PrintingStatusDialog);
  236.  
  237.             if (IsDialogEvent(&anEvent)) {
  238.                 button = DialogSelect(&anEvent, &aDialog, &item);
  239.  
  240.                 if ((button) && (aDialog == PrintingStatusDialog)) {
  241.                     switch (item) {
  242.                         case 1:
  243.                             HiliteControl(pauseButton, 0);        /* Enable PAUSE    */
  244.                             HiliteControl(proceedButton, 255);    /* Disable PROCEED */
  245.                             paused = false;
  246.                             break;
  247.                         case 2:
  248.                             HiliteControl(pauseButton, 255);    /* Disable PAUSE  */
  249.                             HiliteControl(proceedButton, 0);    /* Enable PROCEED */
  250.                             paused = true;
  251.                             break;
  252.                         case 3:
  253.                             PrSetError(iPrAbort);               /* CANCEL printing */
  254.                             paused = false;
  255.                             break;
  256.                     }
  257.                 }
  258.             }
  259.         }
  260.     } while (paused != false); 
  261.  
  262.     SetPort(oldPort);
  263. }
  264.  
  265.  
  266.  
  267. /*****************************************************************************/
  268.  
  269.  
  270.  
  271. #pragma segment Print
  272. OSErr    PresentStyleDialog(FileRecHndl frHndl)
  273. {
  274.     OSErr        err;
  275.     THPrint        prRecHndl;
  276.  
  277.     if (!(prRecHndl = (THPrint)NewHandle(sizeof(TPrint))))
  278.         return(memFullErr);
  279.  
  280.     PrOpen();
  281.  
  282.     if (!(err = PrError())) {
  283.  
  284.         BlockMove((Ptr)&(*frHndl)->doc.print, (Ptr)*prRecHndl, sizeof(TPrint));
  285.             /* Get data, valid or not. */
  286.  
  287.         if (!(*frHndl)->doc.printRecValid) PrintDefault(prRecHndl);
  288.         else                                PrValidate(prRecHndl);
  289.         if (!(err = PrError())) {
  290.             if (PrStlDialog(prRecHndl)) {
  291.                 BlockMove((Ptr)*prRecHndl, (Ptr)&(*frHndl)->doc.print, sizeof(TPrint));
  292.                 (*frHndl)->doc.printRecValid  = true;
  293.                 (*frHndl)->fileState.docDirty = true;
  294.             }
  295.             else err = userCanceledErr;
  296.         }
  297.     }
  298.  
  299.     DisposHandle((Handle)prRecHndl);
  300.     PrClose();
  301.  
  302.     return(err);
  303. }
  304.  
  305.  
  306.  
  307.